package weaver.interfaces.hggd.zjs.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.doc.mobile.systemDoc.util.SystemDocUtil;
import com.wbi.util.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.hggd.zjs.Util.AMSUtil;
import weaver.interfaces.hggd.zjs.Util.LogUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.zijianshe
 * @version: 1.0
 * @CreateDate: 2023-10-24
 * @Description: 收文传对方系统
 **/
public class WeaSW_Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        JSONObject paramJson = new JSONObject();
        JSONObject data = new JSONObject();
        writeLog(">>>>>WeaSW_Action>>>>" + requestid);
        User user = requestInfo.getRequestManager().getUser();

        try {
            String workflowName = getNameByid("workflow_requestbase", "requestname", "requestid", requestid);
            rs.executeQuery("select * from " + tablename + " where requestid=? ", requestid);
            rs.next();
            String ngr = Util.null2String(rs.getString("djr"));
            String djrq = Util.null2String(rs.getString("djrq"));
            String hjcd = Util.null2String(rs.getString("hjcd"));
            String lwdw = Util.null2String(rs.getString("lwdw"));
            String lwwh = Util.null2String(rs.getString("lwwh"));
            String lwxcrq = Util.null2String(rs.getString("lwxcrq"));
            String lwztlx = Util.null2String(rs.getString("lwztlx"));
            String cbrq = Util.null2String(rs.getString("cbrq"));
            String zw = Util.null2String(rs.getString("zwwj"));
            writeLog(">>>>>WeaSW_Action>>>>zw=" + zw + "</br>");
            String wjfs = Util.null2String(rs.getString("wjfs"));
            String wjys = Util.null2String(rs.getString("wjys"));
            String fjfs = Util.null2String(rs.getString("fjfs"));
            String bz = Util.null2String(rs.getString("bz"));
            String fs = Util.null2String(rs.getString("fs"));
            String fjkxz = Util.null2String(rs.getString("fj"));
            String ngrxm = this.getNameByid("hrmresource", "lastname", "id", ngr);
            String zwmc = this.getNameByid("docdetail", "docsubject", "id", zw);
            data.put("dataId", requestid);
            data.put("doctype", "收文");
            data.put("title", workflowName);
            data.put("author", ngrxm);
            data.put("authorld", ngr);
            data.put("docdate", djrq);
            data.put("jjcd", hjcd);
            data.put("filetype", lwztlx);
            data.put("issuedtocompany", lwdw);
            data.put("docnumber", lwwh);
            data.put("date", lwxcrq);
            data.put("undertakedate", cbrq);
            data.put("wjfs", wjfs);
            data.put("pagenum", wjys);
            data.put("fjfs", fjfs);
            data.put("memo", bz);
            data.put("textname", zwmc);
            JSONArray fileList = new JSONArray();
            String annexname = "";
            rs.executeQuery("select imagefileid,'url',imagefilename,filesize,imagefiletype from imagefile where " +
                    "imagefileid in (select imagefileid from docimagefile where docid in (" + fjkxz + "))");

            JSONObject fileJSON;
            String code;
            String message;
            String ddcode;
            while (rs.next()) {
                fileJSON = new JSONObject();
                code = Util.null2String(rs.getString("imagefileid"));
                message = Util.null2String(rs.getString("imagefilename"));
                annexname = "".equals(annexname) ? message : annexname + "," + message;
                ddcode = this.getDDCode(user, code);
                fileJSON.put("fileId", code);
                fileJSON.put("filePath", "http://200.162.8.68:9000/weaver/weaver.file.FileDownload?fileid=" + code + "&download=1&ddcode=" + ddcode);
                fileJSON.put("fileName", message);
                fileJSON.put("fileSize", Util.null2String(rs.getString("filesize")));
                this.writeLog(">>>>>imagefilename>>>>" + message + "</br>");
                fileJSON.put("fileType", message.substring(message.lastIndexOf(".") + 1, message.length()));
                fileList.add(fileJSON);
            }

            rs.executeQuery("select imagefileid,'url',imagefilename,filesize,imagefiletype from imagefile where imagefileid in (select imagefileid from docimagefile where docid in (" + zw + "))", new Object[0]);

            while(rs.next()) {
                fileJSON = new JSONObject();
                code = Util.null2String(rs.getString("imagefileid"));
                message = Util.null2String(rs.getString("imagefilename"));
                ddcode = this.getDDCode(user, code);
                fileJSON.put("fileId", code);
                fileJSON.put("filePath", "http://200.162.8.68:9000/weaver/weaver.file.FileDownload?fileid=" + code + "&download=1&ddcode=" + ddcode);
                fileJSON.put("fileName", message);
                fileJSON.put("fileSize", Util.null2String(rs.getString("filesize")));
                this.writeLog(">>>>>imagefilename>>>>" + message + "</br>");
                fileJSON.put("fileType", message.substring(message.lastIndexOf(".") + 1, message.length()));
                fileList.add(fileJSON);
            }

            data.put("annexname", annexname);
            data.put("fileList", fileList);
            paramJson.put("app", "文书");
            paramJson.put("workflowName", workflowName);
            paramJson.put("workflowId", requestid);
            paramJson.put("data", data);
            AMSUtil var36 = new AMSUtil();
            JSONObject ResponseJson;
            try {
                this.writeLog("发文流程接口参数：" + paramJson);
                code = AMSUtil.send(paramJson.toString());
                LogUtil.InsertLog("", paramJson.toString(), code, "", requestid);
                if ("0".equals(code)) {
                    return "1";
                }
                ResponseJson = JSONObject.parseObject(code);
            } catch (Exception var34) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("接口调用失败，请联系管理员");
                return "0";
            }
            code = ResponseJson.getString("code");
            message = ResponseJson.getString("message");
            if (!"1".equals(code)) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("接口调用失败，失败原因：" + message);
                return "0";
            } else {
                return "1";
            }
        } catch (Exception var35) {
            var35.printStackTrace();
            this.writeLog("异常参考:" + var35);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return "0";
        }
    }

    /**
     * 通过传的字段值查询 字段名
     * @param tablename
     * @param queryName
     * @param id
     * @param value
     * @return
     */

    public String getNameByid(String tablename, String queryName, String id, String value) {
        if ("".equals(value)) {
            return "";
        } else {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select " + queryName + " from " + tablename + " where " + id + "=?", new Object[]{value});
            return rs.next() ? weaver.general.Util.null2String(rs.getString(queryName)) : "";
        }
    }

    public String getDDCode(User user, String fileid) {
        Map params = new HashMap();
        params.put("timelimit", "0");
        params.put("adminright", "1");
        String ddcode = SystemDocUtil.takeddcode(user, fileid, params);
        return ddcode;
    }

}

